/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *SdProjectReviewController
 * SD项目复盘结项
 *
 * 修改纪录
 * 2021-12-10 版本：1.0 jiangtao 创建。
 * @version 版本：1.0
 * @author 作者：jiangtao
 * 创建日期：2021-12-10
 */

package com.topscomm.gpm.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.topscomm.cbo.service.ICboApprovalNodeInsService;
import com.topscomm.gpm.controller.auto.GpmProjectReviewControllerAuto;
import com.topscomm.pub.util.*;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.gpm.pojo.GpmProjectEntity;
import com.topscomm.gpm.pojo.GpmProjectReviewEntity;
import com.topscomm.gpm.service.impl.GpmPMOStandardizationRecordService;
import com.topscomm.gpm.service.impl.GpmProjectService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: jiangtao
 * @date: 2021-12-10
 * @description:SD项目复盘结项Controller
 * @modify:
 */
@RestController
@RequestMapping("sd/projectReview")
public class GpmProjectReviewController extends GpmProjectReviewControllerAuto implements IGpmProjectReviewController {
	@Autowired
	protected ICboApprovalNodeInsService cboApprovalNodeInsService;
	@Autowired
	protected GpmPMOStandardizationRecordService gpmPMOStandardizationRecordService;
	@Autowired
	protected GpmProjectService gpmProjectService;
	/**
	 * @author: jiangtao
	 * @date: 2021-12-10
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_docno")) {
			String value = paraMap.get("qry_docno").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docno like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_begindocdate")) {
			String value = paraMap.get("qry_begindocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
		}
		if (paraMap.containsKey("qry_enddocdate")) {
			String value = paraMap.get("qry_enddocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
		}
		if (paraMap.containsKey("qry_deptname")) {
			String value = paraMap.get("qry_deptname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (deptid in ( SELECT id from CboDept where fullname like '%{0}%'))", value);
		}
		if (paraMap.containsKey("qry_username")) {
			String value = paraMap.get("qry_username").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (username like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_deptcode")) {
			String value = paraMap.get("qry_deptcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcode like '{0}%'", value);
		}
		// 自己添加
		if (paraMap.containsKey("qry_projectname")) {
			String value = paraMap.get("qry_projectname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and projectname like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_projectcode")) {
			String value = paraMap.get("qry_projectcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and projectcode like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_where")) {
			String value = paraMap.get("qry_where").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}", value);
		}
		return sql.toString();
	}

	/**
	 * @author: zhangjunyao
	 * @date: 2022-01-17
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句（项目复盘、复盘归档使用）
	 * @modify:
	 */
	protected String spellListSqlForIndexAndFile(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_projectname")) {
			String value = paraMap.get("qry_projectname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}.projectname like '%{1}%'", GpmProjectEntity.tableName, value);
		}
		if (paraMap.containsKey("qry_projectcode")) {
			String value = paraMap.get("qry_projectcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}.projectcode like '%{1}%'", GpmProjectEntity.tableName, value);
		}
		if (paraMap.containsKey("qry_stateforindex")) {
			String value = paraMap.get("qry_stateforindex").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}.state={1}", GpmProjectReviewEntity.tableName, value);
		}
		if (paraMap.containsKey("qry_open")) {
			String value = paraMap.get("qry_open").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and exists (select 1 from GPMProject where GpmProjectReview.projectid = " +
						"GPMProject.id and GPMProject.open = {0})", value);
		}
		return sql.toString();
	}

	/**
	 * @author: jiangtao
	 * @date: 2021-12-10
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 *
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: jiangtao
	 * @date: 2021-12-10
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}{1}于{2}提交GPM项目复盘结项申请,单据编号{3}! ", dataMap.get("dispdeptcommoncode1"), dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
		dataMap.put("maintitle", mailtitle.toString());
	}

	/***
	 * @description: 新增和编辑操作
	 * @param map
	 * @return
	 * @author: jiangtao1
	 * @date: 2021/12/14 9:14
	 * @modify:
	 */
	@RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT }, value = "doSave")
	public ResponseResult<Object> doSave(@RequestBody(required = false) Map<String, Object> map) {
		try {
			Map<String, Object> resultMap = this.service.doSave(map);
			return ResponseResult.ok(resultMap);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/***
	 * @description: 复盘归档list方法
	 * @param paraMap
	 * @return
	 * @author: zhangjunyao
	 * @date: 2021/12/23
	 * @modify:
	 */
	@GetMapping({"/listForOpen"})
	public ResponseResult<Object> listForOpen(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSqlForIndexAndFile(paraMap);
			Map<String, Object> sqlMap = new HashMap<>();
			sqlMap.put("whereSql", sqlWhere);
			int totalCount = this.service.getCount(sqlMap, "getCountForFile");
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(sqlMap, "queryMapForFile");
			} else {
				tempList = this.service.queryMapByWhere(sqlWhere, "queryMapForFile");
			}
			this.service.setDispFields(tempList);
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return this.handleControllerException(ex);
		}
	}

	/***
	 * @description: 复盘重写list方法(仅给index组件用）
	 * @param paraMap
	 * @return
	 * @author: zhangjunyao
	 * @date: 2021/12/23
	 * @modify:
	 */
	@GetMapping({"/listForIndex"})
	public ResponseResult<Object> listForIndex(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSqlForIndexAndFile(paraMap);
			Map<String, Object> sqlMap = new HashMap<>();
			sqlMap.put("whereSql", sqlWhere);
			int totalCount = this.service.getCount(sqlMap, "getCountForIndex");
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(sqlMap, "queryMapForIndex");
			} else {
				tempList = this.service.queryMapByWhere(sqlWhere, "queryMapForIndex");
			}
			this.service.setDispFields(tempList);
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for(Map<String, Object> map : tempList) {
				if("9".equals(ConvertUtil.convertToString(map.get("state")))) {
					map.put("endtime", ConvertUtil.convertToDate(map.get("modifiedon")));
				} else {
					map.put("endtime", "");
				}
			}
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return this.handleControllerException(ex);
		}
	}

	/**
	 * @author: zhangjunyao
	 * @date: 2021-12-23
	 * @description: 修改项目是否公开的权限
	 * @modify:
	 */
	@PutMapping({"/changePermission"})
	public ResponseResult<Object> changePermission(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			String projectId = paraMap.get("projectid").toString();
			String message = this.service.changePermission(projectId);
			return ResponseResult.ok(message);
		} catch (Exception e) {
			return this.handleControllerException(e);
		}
	}

	/**
	 *
	 * @param paraMap
	 * @author zhangjunyao
	 * @date 2022-01-19
	 * @description 判断是否已经提交复盘
	 * @return boolean
	 */
	@GetMapping(value = "/ifReviewed")
	public ResponseResult<Object> ifReviewed(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			String projectId = "";
			if (paraMap.containsKey("projectid")) {
				projectId = ConvertUtil.convertToString(paraMap.get("projectid"));
			}
			boolean ifReviewed = this.service.ifReviewed(projectId);
			return ResponseResult.ok(ifReviewed);
		} catch (Exception ex) {
			return this.handleControllerException(ex);
		}
	}

	/**
	 *
	 * @param paraMap
	 * @author hdy
	 * @date 2022-03-24
	 * @description 项目概述增加系统自定义内容:本项目于xx年xx月xx日中标/签订合同/提交借用申请，
	 * 中标/签订合同/借用金额为xx万元（涉及xx产品xx只），
	 * 于xx年xx月xx日完成项目立项。
	 * 项目目标为：xxxxx。
	 * 项目执行过程中存在项目超期、n个里程碑超期、n个流程超期、n个任务超期 ，
	 * 最终项目于xx年xx月xx日完成交付xx万元（xx产品xx只），实际履约至m%。
	 * @return map
	 */
	@GetMapping(value = "/addsummary")
	public ResponseResult<Object> addsummary(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			String newsummary = this.service.addsummary(paraMap);
			Map<String, Object> resultmap = new HashMap<>();
			resultmap.put("newsummary", newsummary);
			return ResponseResult.ok(resultmap);
		} catch (Exception ex) {
			return this.handleControllerException(ex);
		}
	}
}

